﻿#region Copyright
/* Product of DTL Software
 * Product Name : Support Memmory
 * Author : dinhtienlam.spkt@gmail.com
 * About: Developer of DTL Software
 * Contact : 0966 330 237
 */
#endregion

#region Using Default
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Resources;
using System.Reflection;
using System.Configuration;
using System.Globalization;
using System.Collections.Generic;
#endregion

#region Add Reference of MVC#
using Lamdt.MVCSharp.Core;
using Lamdt.MVCSharp.Core.Tasks;
using Lamdt.MVCSharp.Core.Configuration.Tasks;
#endregion

namespace Lamdt.LogicLayer
{
    public class dtlBaseTask : TaskBase
    {
        #region Language of application
        public static CultureInfo ciEN = new CultureInfo(Task.logicConfigs.Default.EN);
        public static CultureInfo ciVN = new CultureInfo(Task.logicConfigs.Default.VI);
        #region Documentation
        /// <summary>
        /// Common varial store resource of application.
        /// </summary> 
        #endregion
        public static readonly ResourceManager spmResourceMan =
                            new ResourceManager(Task.logicConfigs.Default.ResourceName,
                                                        Assembly.GetExecutingAssembly());
        #region Documentation
        /// <summary>
        /// Public event for change language of application.
        /// </summary> 
        #endregion
        public event EventHandler LanguageChange;

        #region Documentation
        /// <summary>
        /// Current culture information
        /// </summary> 
        #endregion
        private CultureInfo _currentCulture;
        public CultureInfo CurrentCulture
        {
            set
            {
                if (_currentCulture == value)
                    return;
                _currentCulture = value;
                if (LanguageChange != null)
                    LanguageChange(this, EventArgs.Empty);
            }
            get
            {
                if (_currentCulture == null)
                {
                    _currentCulture = ciEN;
                }
                return _currentCulture;
            }
        }

        #region Documentation
        /// <summary>
        /// Current language of system
        /// </summary> 
        #endregion
        public virtual string CurrentLanguage
        {
            set
            {
                if (string.IsNullOrEmpty(value) ||
                    CurrentLanguage.Equals(value))
                    return;
                else
                {
                    CurrentCulture = new CultureInfo(value);
                    Thread.CurrentThread.CurrentCulture = CurrentCulture;
                    Thread.CurrentThread.CurrentUICulture = CurrentCulture;
                }
            }

            get
            {
                return CurrentCulture.Name;
            }
        }

        #region Documentation
        /// <summary>
        /// Get text recording current language
        /// </summary>
        /// <param name="textKey">Keyword</param>
        /// <returns>Text in current culture</returns> 
        #endregion
        public string GetText(string textKey)
        {
            return spmResourceMan.GetString(textKey, CurrentCulture);
        } 
        #endregion
    }
}
